package other.pourwater;

import java.util.Date;
import java.util.Scanner;

public class PourWater {
	private WaterContainer[] containers;
	private int step;
	private Scanner in;
	private boolean stopped;
	
	public PourWater(int... containerSizes) {
		containers = new WaterContainer[containerSizes.length];
		for (int i = 0; i < containers.length; ++i) {
			containers[i] = new WaterContainer(containerSizes[i]);
		}
		
		step = 1;
		in = new Scanner(System.in);
		stopped = true;
	}
	
	public void printState() {
		for (int i = 0; i < containers.length; ++i) {
			System.out.println("B" + i + " " + containers[i]);
		}
	}
	
	public void start() {
		step = 1;
		stopped = false;
		
		while (!stopped) {
			parseCommand(prompt());
		}
	}
	
	public void stop() {
		stopped = true;
		System.out.println("Byebye!");
	}
	
	private void pourWater(int from, int to) {
		System.out.println("Pouring from B" + from + " to B" + to);
		containers[from].pourWaterTo(containers[to]);
		printState();
	}
	
	private void fillWater(int to) {
		System.out.println("Filling B" + to);
		containers[to].fill();
		printState();
	}
	
	
	
	private void parseCommand(String command) {
		if (command.trim().startsWith("p")) {
			String[] array = command.split(" ");
			if (array.length == 3) {
				int from = Integer.parseInt(array[1]);
				int to = Integer.parseInt(array[2]);
				pourWater(from, to);
				++step;
			}
			else {
				System.err.println("Wrong command!");
			}
		}
		else if (command.trim().startsWith("f")) {
			String[] array = command.split(" ");
			if (array.length == 2) {
				int to = Integer.parseInt(array[1]);
				fillWater(to);
				++step;
			}
			else {
				System.err.println("Wrong command!");
			}
		}
		else if (command.trim().equals("END")) {
			stop();
		}
	}
	
	private String prompt() {
		System.out.print("Step " + step + " - Enter your instruction: ");
		return in.nextLine();
	}
	
	public static void main(String[] args) {
//		PourWater pourWater = new PourWater(8, 9, 4);
//		pourWater.start();
		
		
		
		long time = 1553700 * 1000;
		Date d = new Date(time);
		System.out.println(d);
	}
	
}

class WaterContainer {
	private int volume;
	private int volumeWater;
	
	public WaterContainer(int volume) {
		this.volume = volume;
	}
	
	public void pourWaterTo(WaterContainer other) {
		int emptyVolume = other.volume - other.volumeWater;
		if (volumeWater > emptyVolume) {
			// Fill container "other"
			other.volumeWater = other.volume;
			volumeWater -= emptyVolume;
		}
		else {
			other.volumeWater += volumeWater;
			volumeWater = 0;
		}
	}
	
	public void fill() {
		volumeWater = volume;
	}
	
	@Override
	public String toString() {
		StringBuilder rep = new StringBuilder();
		rep.append("(");
		rep.append(volumeWater);
		rep.append("L/");
		rep.append(volume);
		rep.append("L) [");
		for (int i = 1; i <= volume; ++i) {
			if (i <= volumeWater ) {
				rep.append("-");
			}
			else {
				rep.append(" ");
			}
		}
		rep.append("]");
		
		return rep.toString();
	}
	
}